package com.linran.common;

import org.junit.Test;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 *     //返回连接子进程正常输入的输出流
 *     abstract public OutputStream getOutputStream();
 *
 *     //返回连接子进程输出的输入流
 *     abstract public InputStream getInputStream();
 *
 *     //返回连接子进程异常输出的输入流
 *     abstract public InputStream getErrorStream();
 *
 *     //促使当前线程等待，直至只当进程已经结束。子进程结束时函数立即返回
 *     abstract public int waitFor() throws InterruptedException;
 *
 *     //返回子进程结束时候的退出值
 *     abstract public int exitValue();
 *
 *     //杀死子进程
 *     abstract public void destroy();
 */
public class ProcessBuilderLearn {
    //执行java程序
    @Test
    public void sample1() throws IOException {
        List<String> commands = new ArrayList<>();
        commands.add("java");
        commands.add("CharsetConvert");

        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command(commands);
        File file = new File("E:\\java资料\\代码\\linran\\common\\target\\classes\\com\\linran\\common");
        processBuilder.directory(file);  //切换到工作目录
        //processBuilder.redirectErrorStream(true);
        Process process = null;
        try {
            process = processBuilder.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

        String result = null;
        String errorresult = null;
        InputStream in = process.getInputStream(); //得到命令执行的流
        BufferedReader br = new BufferedReader(new InputStreamReader(in,Charset.forName("GBK")));//这里GBK为了解决乱码问题

        InputStream error = process.getErrorStream(); //得到命令执行的错误流
        BufferedReader errorbr = new BufferedReader(new InputStreamReader(error,Charset.forName("GBK")));//这里GBK为了解决乱码问题
        String lineStr;
        while ((lineStr = br.readLine()) != null) {
            result = lineStr;
        }

        br.close();
        in.close();
        //System.out.println("result: " + new String(result.getBytes(GBK.defaultCharset()),Charset.forName("UTF-8")));
        System.out.println("result: " + result);
        while ((lineStr = errorbr.readLine()) != null) {
            errorresult = lineStr;
        }
        errorbr.close();
        error.close();
        System.out.println("errorresult: " + errorresult);

        try {
            final int status = process.waitFor(); //阻塞，直到上述命令执行完!!!!(返回值为exitValue)
            process.destroy(); //杀死子进程
            //@code 0 indicates normal termination.
            if (status == 0){
                System.out.println("成功执行~");
            } else {
                //TODO:异常处理
                System.out.println("异常了");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("执行结束");
        }
    }

    /**
     * 执行终端命令
     * @throws IOException
     */
    @Test
    public void terminal() throws IOException {
        ProcessBuilder pb = new ProcessBuilder("ls");
        Process process = pb.start();
        String line;
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        reader.close();
    }

    @Test
    public void exe() throws IOException {
        ProcessBuilder p = new ProcessBuilder("C:/Program Files/Notepad++/Notepad++.exe");
        p.start();
    }
}
